<template>

  <el-container>

    <el-header>

    </el-header>

    <el-container>

      <el-aside width="200px">
        <div style="height: 300px;">
          <el-steps direction="vertical" :active="active" finish-status="success">
            <el-step title="步骤 1"></el-step>
            <el-step title="步骤 2"></el-step>
            <el-step title="步骤 3"></el-step>
          </el-steps>
        </div>
      </el-aside>

      <el-main>

        <div :class="active===0?'':'hiden'">
          <el-form label-position="top" label-width="130px" :model="first">
            <el-form-item label="用户名/手机号:">
              <el-input v-model="first.username" placeholder="请输入手机号或者用户名"></el-input>
            </el-form-item>
            <el-form-item label="验证方式:" prop="region">
              <el-select v-model="first.verify_way" placeholder="请选择验证方式">
                <el-option label="手机验证" value="mobile"></el-option>
                <el-option label="邮箱验证" value="email"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>

        <div :class="active===1?'':'hiden'">
          <div :class="first.verify_way==='email'?'':'hiden'">
            <el-form label-position="top" label-width="130px" :model="first">
              <el-form-item label="邮箱地址:">
                <el-input disabled type="email" v-model="first.email" placeholder="未绑定邮箱"></el-input>
              </el-form-item>
              <el-form-item label="验证码:">
                <el-input v-model="first.email" placeholder="请输入验证码"></el-input>
              </el-form-item>
            </el-form>
          </div>
          <div :class="first.verify_way==='mobile'?'':'hiden'">
            <el-form label-position="top" label-width="130px" :model="first">
              <el-form-item label="手机号码:">
                <el-input disabled v-model="first.mobile"></el-input>
              </el-form-item>
              <el-form-item label="验证码:">
                <el-input v-model="first.mobile" placeholder="请输入验证码"></el-input>
              </el-form-item>
            </el-form>
          </div>
        </div>

        <div :class="active===2?'':'hiden'">
          <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="密码" prop="pass">
              <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="年龄" prop="age">
              <el-input v-model.number="ruleForm.age"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-button style="margin-top: 12px;" @click="next">下一步</el-button>

      </el-main>

    </el-container>

  </el-container>
</template>

<script>
  export default {
    data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('年龄不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value < 18) {
              callback(new Error('必须年满18岁'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        active: 0,
        first: {
          username: '',
          verify_way: 'mobile',
        },
        second: {
          mobile: '',
          email: '',
        },
        ruleForm: {
          pass: '',
          checkPass: '',
          age: '',
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          age: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
      };
    },

    methods: {
      next() {
        if (this.active++ > 2) this.active = 0;
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped>
  .hiden{
    display: none;
  }
</style>
